<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\tool\Result;
use think\App;
use think\facade\Cache;
use think\facade\View;
use think\Response;

class Theme extends Base
{
    public function index(): string
    {
        $theme = self::getTheme();
        $list = config('theme');
        View::assign([
            'theme' => $theme,
            'list' => $list,
            'colorList' => $list[$theme['version']]['color']
        ]);

        return View::fetch();
    }

    public function color($version): Response
    {
        $list = config('theme')[$version]['color'];

        return Result::success('success', array_values($list));
    }

    public function change(): Response
    {
        $param = $this->request->post();
        $theme = [
            'version' => $param['version'],
            'color' => $param['color']
        ];
        Cache::set('theme', $theme);

        return Result::success('切换成功');
    }

    public static function getTheme(): array
    {
        $theme = Cache::get('theme');
        if (empty($theme)) {
            $theme = [
                'version' => env('THEME_VERSION', 'v1'),
                'color' => env('THEME_COLOR', 'red')
            ];
            Cache::set('theme', $theme);
        }

        return $theme;
    }
}